package com.gcxy.conf.exception.handler;

import com.gcxy.conf.exception.annotation.GlobalException;
import com.gcxy.conf.exception.exception.IGlobalException;
import com.gcxy.conf.result.R;
import com.gcxy.conf.result.ResultCodeEnum;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 方法参数类型不匹配异常
 *
 * @author shineTan
 * @date 2022/12/26 14:28
 */
@GlobalException(throwable = MethodArgumentTypeMismatchException.class)
public class MethodArgumentTypeMismatchExceptionHandler implements IGlobalException<R, MethodArgumentTypeMismatchException> {

    @Override
    public R exception(MethodArgumentTypeMismatchException throwable, HttpServletRequest request, HttpServletResponse response) {
        return BaseThrowExceptionHandler.exception(throwable, ResultCodeEnum.REQUEST_PARAM_ERROR, () ->
                String.format("请求参数名:%s,类型:%s,请求参数值:%s与参数类型不匹配", throwable.getName(), throwable.getRequiredType().getSimpleName(), throwable.getValue()));
    }
}
